<?php
// ### AclAssistant : Item
class Action_Community_ShowIssues extends RL_Handler
{
	public function getWidgetsConfig ()
	{

		$widgets = array(
			'0' => array (
				'name' => 'CreateBtnWidget',
				'params' => array(
					'widget_position' => 'createbtn',
					'widget_view' => 'WG_CreateBtnWidget',
				),
			),
		);
		return $widgets;
	} // end of function getWidgetsConfig

	protected function init() {
		$this->rh->UseClass('RL_CyrDate');
		$this->aa = $this->module->aa;

		$this->data = $this->aa->getInfo();
		// HACK : 2008-01-18 : lucky {{{
		// если в разделе, смотрим операции выставленные разделом, иначе -- сообществом.
		$object_oid = isset($this->data['tdi_oid']) ? $this->data['tdi_oid'] : $this->data['comity_oid'];
		$this->aa->getAvailableOperations($object_oid);
		//$this->aa->getAvailableOperations();
		// }}}

		$this->ctx->mm->setAclAssistant( $this->aa );
		$this->am_item = $this->ctx->mm->getModel("Item", "a");
		$this->am_item->page_citems = 5;

		$this->m_tdi = $this->ctx->mm->getModel("Tdi", "m");
		$tdi_oids = array();
		foreach ($this->aa->acl_data['tdi_list'] as $tdi) $tdi_oids[] = $tdi['id_object'];
		$this->tdi_types = $this->m_tdi->getSections($tdi_oids);



		// --- tags --- {{{
		// контроллер тегов
		// попросили выбирать по тегу?
		$tag = NULL;
		if (isset($this->rh->GET['tag'])) {
			$tag_name = $this->rh->GET['tag'];
			$tag = $this->rh->ctx->store->find( 'select objects where tag.name=:0', array($tag_name))->next();
		}
		// --- tags --- }}}

		$this->tag = $tag;

		// --- sections --- {{{
		$tdis = array_map('intval', array_keys($this->rh->GET['tdi']));
		$tdis = array_combine($tdis, $tdis); // value => value
		// если установлена галка "Все", то все остальное херим
		if (array_key_exists(0, $tdis)) unset($tdis[0]);
		// --- sections --- }}}

		if ($tdis) {
			$this->tdis = $tdis;
		} elseif ($this->data['tdi_oid']) {
			$this->tdis[] = $this->data['tdi_oid'];
		}

		if ($this->rh->GET['filter_by_day']) $this->filter_by_day = $this->rh->GET['filter_by_day'];

	}

	public function process ()
	{
		$aa = &$this->aa;

		$page_number = isset($this->rh->GET['p'])?$this->rh->GET['p']:1;

		$aa->checkDeletions(array($this, "shutdown") );

		$this->am_item->SetModelParams($this->tag, $this->tdis, $page_number, $this->filter_by_day);
		list($years, $pager) = $this->am_item->getItems('years');

		$view = $this->rh->views->findByName('issues');
		$view->data = $aa->template_data;

		$view->data['can_create'] = $aa->is_granted(array('item_create', 'item_create_premod'));
		if ($years) {
			$view->data['years'] = $years;
		} else $view->data['message'] = "В этой ленте нет сообщений.";

		$view->data['page'] = $pager;

		if ( isset($this->params['tdi_url']) ) {
			$view->data['defaultsection'] = $this->params['tdi_url'];
			$view->data['tdi_title'] = $this->tdi_types[ $this->data['tdi_oid'] ]['title'];
			$view->data['tdi_color'] = $this->tdi_types[ $this->data['tdi_oid'] ]['color'];
			$view->data['tdi'] = $this->tdi_types[ $this->data['tdi_oid'] ];
		}

		$items_out = $this->am_item->page_citems; // кол-во итемов отправленных юзеру
		$items_remain = $this->am_item->items_count - $items_out;

		if ( $items_remain >= $this->am_item->page_citems ) {
			$view->data['items_remaining'] = $this->am_item->page_citems;
		} else $view->data['items_remaining'] = $items_remain;

		$view->data['__widgets__'] = $this->widgets;

		$view->data['show_item_create_button'] = 1;


		$this->rh->response->setContent( $view );

		// -- touch visitor stat -- {{{
		// visit  tdi
		$this->ctx->mm->getModel("Visitable", "m")->getVisitable('Tdi')->visit();
		//
		// -- touch visitor stat -- }}}

	} // end of function execute

}
